// 表单校验规则由 schema2code 生成，不建议直接修改校验规则，而建议通过 schema2code 生成, 详情: https://uniapp.dcloud.net.cn/uniCloud/schema


const validator = {
  "type_id": {
    "rules": [
      {
        "required": true
      },
      {
        "format": "string"
      }
    ],
    "title": "服务类型",
    "label": "服务类型"
  },
  "customer_needs": {
    "rules": [
      {
        "format": "string"
      }
    ],
    "title": "服务需求描述",
    "label": "服务需求描述"
  },
  "customer_phone": {
    "rules": [
	  {
	    "required": true
	  },
      {
        "format": "string"
      },
      {
        "maxLength": 11
      },
      {
        "minLength": 11
      }
    ],
    "title": "联系电话",
    "label": "联系电话"
  },
  "store_id": {
    "rules": [
      {
        "required": true
      },
	  {
        "format": "string"
      },
      {
        "minLength": 1
      }
    ],
    "title": "宇壹联盟门店",
    "label": "宇壹联盟门店"
  },
  "offer_price": {
    "rules": [
      {
        "format": "double"
      },
      {
        "minimum": 0
      }
    ],
    "title": "报价",
    "label": "报价"
  },
  "auditor_id": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "butler_id": {
    "rules": [
      {
        "required": true
      },
	  {
        "format": "string"
      },
      {
        "minLength": 1
      }
    ]
  },
  "supervisor_id": {
    "rules": [
      {
        "required": true
      },
	  {
        "format": "string"
      },
      {
        "minLength": 1
      }
    ]
  },
  "officer_id": {
    "rules": [
      {
        "format": "string"
      }
    ]
  },
  "price": {
    "rules": [
      {
        "required": true
      },
	  {
        "format": "double"
      },
      {
        "minimum": 1
      }
    ],
    "title": "总价格",
    "label": "总价格"
  },
  "butler_income": {
    "rules": [
      {
        "format": "double"
      },
      {
        "minimum": 0
      }
    ],
    "title": "预计收益",
    "label": "预计收益"
  },
  "license_photos": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 512
      }
    ]
  },
  "service_photos": {
    "rules": [
      {
        "format": "array"
      }
    ]
  },
  "payment_order_id": {
    "rules": [
      {
        "format": "string"
      },
      {
        "maxLength": 50
      }
    ],
    "title": "商户订单号",
    "label": "商户订单号"
  },
  "status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "已关闭",
            "value": -1
          },
          {
            "text": "审核中",
            "value": 0
          },
          {
            "text": "派单中",
            "value": 1
          },
          {
            "text": "服务中",
            "value": 2
          },
          {
            "text": "待缴费",
            "value": 3
          },
          {
            "text": "核算中",
            "value": 4
          },
          {
            "text": "已完成",
            "value": 5
          }
        ]
      }
    ],
    "title": "服务单状态",
    "defaultValue": 0,
    "label": "服务单状态"
  },
  "paymant_status": {
    "rules": [
      {
        "format": "int"
      },
      {
        "range": [
          {
            "text": "未支付",
            "value": 0
          },
          {
            "text": "已支付",
            "value": 1
          },
          {
            "text": "已审核",
            "value": 2
          }
        ]
      }
    ],
    "title": "支付状态",
    "defaultValue": 0,
    "label": "支付状态"
  }
}

const enumConverter = {
  "status_valuetotext": {
    "0": "审核中",
    "1": "备货中",
    "2": "进行中",
    "3": "待缴费",
    "4": "核算中",
	"5": "已完成",
    "-1": "已关闭"
  },
  "paymant_status_valuetotext": {
    "0": "未支付",
    "1": "已支付",
    "2": "已审核"
  },
  "score_valuetotext": {
    "1": "介入",
    "3": "",
    "5": "优秀"
  }
}

function filterToWhere(filter, command) {
  let where = {}
  for (let field in filter) {
    let { type, value } = filter[field]
    switch (type) {
      case "search":
        if (typeof value === 'string' && value.length) {
          where[field] = new RegExp(value)
        }
        break;
      case "select":
        if (value.length) {
          let selectValue = []
          for (let s of value) {
            selectValue.push(command.eq(s))
          }
          where[field] = command.or(selectValue)
        }
        break;
      case "range":
        if (value.length) {
          let gt = value[0]
          let lt = value[1]
          where[field] = command.and([command.gte(gt), command.lte(lt)])
        }
        break;
      case "date":
        if (value.length) {
          let [s, e] = value
          let startDate = new Date(s)
          let endDate = new Date(e)
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
      case "timestamp":
        if (value.length) {
          let [startDate, endDate] = value
          where[field] = command.and([command.gte(startDate), command.lte(endDate)])
        }
        break;
    }
  }
  return where
}

export { validator, enumConverter, filterToWhere }
